//*************************************************************************\\
//***********	       	  tdc_automata_drv_init.cpp		***********\\
//*************************************************************************\\
//*************************************************************************\\
//*********** 		        UFPE/Cin - Greco		***********\\
//*********** 		 http://www.cin.ufpe.br/~greco 		***********\\
//***********							***********\\
//***********							***********\\
//*********** 		    By Rafael Melo Macieira		***********\\
//***********							***********\\
//***********		    E-mail: rmm2@cin.ufpe.br		***********\\
//*************************************************************************\\
//*************************************************************************\\
//**		 Automata for the temporal check of the			 **\\
//**	   	 drv_init service's		 			 **\\
//*************************************************************************
\
\

#include <stdio.h>
#include "headers/tdc_automata_drv_init.h"
#include "headers/address.h"

void TDC_Automata_drv_init::printRejection(StateID stateID) {

};

int TDC_Automata_drv_init::checkState(StateID stateID) {
    return 0;
};

int TDC_Automata_drv_init::checkInitialState() {
    return checkState(INITIAL_STATE);
};

TDC_Automata_drv_init::StateID TDC_Automata_drv_init::getNextState(StateID stateID) {
    switch (stateID) {
        case In:
            if (access_type == 0) {
                return In;
            } else {
	      //printf("Valor %d e passou? %d",*lcd_ctrl_reg,GET_REG_DATA_CMD(*lcd_ctrl_reg));
                if (GET_REG_DATA_CMD(*lcd_ctrl_reg) == 1) {
                    return P1;
                }
            }
            break;
        case P1:
            if (access_type == 0) {
                return P1;
            } else {
                if (GET_REG_CMD(*lcd_ctrl_reg) == 1) {
                    return P2;
                }
            }
            break;
        case P2:
            if (access_type == 0) {
                return P2;
            } else {
                if (GET_REG_CMD(*lcd_ctrl_reg) == 17) {
                    return P3;
                }
            }
            break;
        case P3:
            if (access_type == 0) {
                return P3;
            } else {
                if (GET_REG_CMD(*lcd_ctrl_reg) == 3) {
                    return P4;
                }
            }
            break;
        case P4:
            if (access_type == 0) {
                return P4;
            } else {
                if (GET_REG_CMD(*lcd_ctrl_reg) == 19) {
                    return P5;
                }
            }
            break;
        case P5:
            if (access_type == 0) {
                return P5;
            } else {
                if (GET_REG_CMD(*lcd_ctrl_reg) == 33) {
                    return A;
                }
            }
            break;
        case A:
            //if (access_type == 0) {
                return A;
            //} else {
            //    return R;
            //}
            break;
    }
    return R;
};

int TDC_Automata_drv_init::runStep(bool access_type, VarList* mainVarList) {
    //unsigned int *currentVar= currentVarList->getVar();
    //*currentVar =
    this->access_type = access_type;
    this->refreshVariables(mainVarList);
    StateID tmp_currentState = this->getNextState(currentState);
    //printf("Entrou!!\n");
    switch (tmp_currentState) {
        case A:
            printf("Accepted!\n");
            this->currentState = tmp_currentState;
            return 1;
            break;
        case R:
            printf("Rejected!\n");
            this->currentState = tmp_currentState;
            return -1;
            break;
        default:
            this->currentState = tmp_currentState;
            return 0;
    }
};

void TDC_Automata_drv_init::refreshVariables(VarList* mainVarList) {
	lcd_ctrl_reg = mainVarList->getVarListByPlatformAddr(mainVarList,LCD_CTRL_REG_ADDR)->getVar(); //50
};

void TDC_Automata_drv_init::reset(){
	this->currentState = In;
};

TDC_Automata_drv_init::TDC_Automata_drv_init(VarList* mainVarList) : TDC_Automata("DRV Init"){//, mainVarList) {
    this->refreshVariables(mainVarList);
}
